<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/1
 * Time: 13:43
 * 大数据查询类，分析完成直接存入数据库中，第二天才能查看
 */

require 'env.php';

//从数据库查询出来，展示到页面，然后可以分页可根据条件搜索

$dsn = 'mysql:dbname=' . DB_NAME . ';host=' . DB_HOST . ';port=' . DB_PORT;
$user = DB_USER;
$password = DB_PWD;
$dbh = new PDO($dsn, $user, $password);
$dbh->query("set names utf8");
// 获取当前页数
if (isset($_GET['page'])) {
    $page = intval($_GET['page']);
} else {
    $page = 1;
}

if (isset($_GET['search'])) {
    $where = 'num = ' . trim($_GET['num']);
} else {
    $where = '1=1';
}

// 每页数量
$page_size = 10;
// 获取总数据量
$sql1 = "select count(*) as amount from `bd_data_logs` where " . $where;

$row = $dbh->query($sql1)->fetch();
$amount = $row['amount'];
// 记算总共有多少页

$page_count = 0;
if ($amount) {
    if ($amount < $page_size) {
        $page_count = 1;
    } else if ($amount % $page_size) { //取总数据量除以每页数的余数
        $page_count = (int)($amount / $page_size) + 1; //如果有余数，则页数等于总数据量除
        //以每页数的结果取整再加一
    } else {
        $page_count = $amount / $page_size; //如果没有余数，则页数等于总数据量除以每页数的
        //结果
    }
} else {
    $page_count = 0;
}


// 翻页链接
if (isset($_GET['search'])) {
    $page_string = '';
} else {

    $page_string = '';
    if ($page == 1) {
        $page_string .= '<div style="text-align: center;margin-top: 50px;"> <a style="color: blue;" href ="queryLogData.php?page=' . ($page + 1) . '" > 下一页</a > | <a style="color: white;" href ="queryLogData.php?page=' . $page_count . '" > 尾页</a > </div>';
    } else if ($page == $page_count) {
        $page_string .= '<div style="text-align: center;margin-top: 50px;color: blue;"><a style="color: white;" href ="queryLogData.php?page=1" > 首页</a >|<a href ="queryLogData.php?page=' . ($page - 1) . '" > 上一页</a ></div>';
    } else {
        $page_string .= '<div style="text-align: center;margin-top: 50px;color: blue;"><a style="color: white;" href ="queryLogData.php?page=1" > 首页</a >|<a href ="queryLogData.php?page=' . ($page - 1) . '">上一页 </a >|<a
href ="queryLogData.php?page=' . ($page + 1) . '">下一页 </a ><a style="color: white;" href ="queryLogData.php?page=' . $page_count . '" > 尾页</a ></div>';
    }
}
// 获取数据，以二维数组格式返回结果
if ($amount) {
    $sql = "select * from `bd_data_logs` where " . $where . " order by UNIX_TIMESTAMP(order_time) desc limit " . ($page - 1) * $page_size . ",$page_size";
    $rowset = $dbh->query($sql)->fetchAll();
} else {
    $rowset = array();
}
?>


<!DOCTYPE html >
<!--saved from url = (0044)http://ebiz.viewlayer.cn/detail/v2/791/c2/p8 -->
<html>
<head>
    <meta http - equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title>黑猫宅急便大数据查询插件</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <LINK href="style/img/favicon.ico" type="image/x-icon" rel=icon>
    <script src="style/js/jquery.js"></script>

    <style>
        .title {
            text-align: center;
            font-size: 50px;
            margin-top: 80px;
            line-height: 10px;
            color: black;
        }

        .t1 {
            margin-top: 80px;
            margin-left: 100px;
        }

        .t2 {
            margin-top: 30px;
            margin-left: 100px;
        }

        .t3 {
            margin-top: 30px;
            margin-left: 100px;
        }

        .b1 {
            margin-top: 30px;
            margin-left: 100px;
        }

        .hid {
            display: none;
        }

        .tablelist {
            width: 80%;
            font-size: 20px;
            border: 1px solid #d0d0d0;
            line-height: 20px;
            text-align: center;
            margin-left: 10%;
            margin-right: 10%;
            border-spacing: 30px;
        }

        .top {
            width: 50px;
            height: 10px;
            margin-bottom: 5px;
        }

        .nodata {
            margin-top: 20px;
            text-align: center;
        }
    </style>
</head>
<body style=" background: #856d35 none repeat scroll 0 0;">
<div id="pagess">
    <a name="top"></a>

    <div class="title">
        黑猫宅急便大数据查询系统
    </div>

    <div style="text-align: center;margin-top: 60px;">
        <form id="search_form" name="search" method="get" action="queryLogData.php"
              enctype="multipart/form-data">
            <input type="hidden" name="search" value="1"/>
            包裹单号：<input type="text" name="num" value="<?php if (isset($_GET['num'])) echo $_GET['num'] ?>"/>
            <input type="submit" value="搜索"/>
        </form>
    </div>
    <div class="price-item" style="margin-top: 80px;">

        <br/>
        <?php if ($rowset) { ?>
            <table cellpadding="0" cellspacing="0" class="tablelist">
                <tbody>
                <tr class="top" style="color: yellow;font-size: 25px">
                    <td align="center">包裹查詢號碼</td>
                    <td align="center">目前狀態</td>
                    <td align="center">資料登入時間</td>
                    <td align="center">負責營業所</td>
                </tr>

                <?php
                foreach ($rowset as $nosig) {
                    echo '<tr class="top">
                    <td align="center"><a href="#">' . $nosig['num'] . '</a></td>
                    <td align="center">' . $nosig['status'] . '</td>
                    <td align="center">' . $nosig['order_time'] . '</td>
                    <td align="center">' . $nosig['owner_addr'] . '</td>
                </tr>';
                }
                ?>
                </tbody>
            </table>
        <?php } else { ?>
            <div class="nodata" style="color: red;">无数据</div>
        <?php } ?>
        <?php echo $page_string; ?>
    </div>
</div>